package com.goods.controller.business;

import com.goods.business.service.InStockService;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "业务管理-入库记录相关接口")
@RestController
@RequestMapping("/business/inStock")
public class InStockController {

    @Autowired
    private InStockService inStockService;

    @GetMapping("/findInStockList")
    public ResponseBean<PageVO<InStockVO>> findInStockList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                           InStockVO inStockVO){
        //status=1&type=2&inNum=qweqwe&startTime=2021-05-07+00:00:00&endTime=2021-06-03+00:00:00
        List<InStockVO> inistocks = inStockService.findInStockList(inStockVO.getId(),inStockVO.getStatus(),
                inStockVO.getType(), inStockVO.getInNum(),
                inStockVO.getStartTime(),inStockVO.getEndTime());
        List<InStockVO> page = ListPageUtils.page(inistocks, pageSize, pageNum);
        PageVO pageVO = null;
        if(page != null){
            pageVO = new PageVO(page.size(), page);
            return ResponseBean.success(pageVO);
        }else {
            List<Product> list = new ArrayList<>();
            pageVO = new PageVO(0,list);
            return ResponseBean.success(pageVO);
        }
    }

    ///detail/" + id+"?pageNum="+this.pageNum
    @GetMapping("/detail/{id}")
    public ResponseBean<InStockDetailVO> detail(@PathVariable Long id,
                                                @RequestParam Integer pageNum){
        InStockDetailVO inStockDetailVO = inStockService.detail(id);
        return ResponseBean.success(inStockDetailVO);
    }


    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        inStockService.delete(id);
        return ResponseBean.success();
    }
    ///remove/112
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        inStockService.remove(id);
        return ResponseBean.success();
    }

    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        inStockService.back(id);
        return ResponseBean.success();
    }

    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        inStockService.publish(id);
        return ResponseBean.success();
    }

    //入库
    //数据填写后，用户点击立即创建按钮，进行入库单新增，随机生成入库单号
    //新增后的入库单默认为待审核状态
    @PostMapping("/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody @Validated InStockVO InStockVO){
        inStockService.addIntoStock(InStockVO);
        return ResponseBean.success();
    }


}
